import * as step from '@flow-step/step-toolkit'
import * as path from 'path'
import * as fs from 'fs'
import { simpleGit, SimpleGit } from 'simple-git';
import {SetSSHPrivateKey} from "./sshKey"
import {masking} from './util'

const SEP = path.sep;

export async function deleteBranch() : Promise<void>{

    let projectDir = process.env.PROJECT_DIR as string;
    let repoDirName = path.basename(projectDir);  //the repo we work on
    SetSSHPrivateKey(repoDirName);

    process.env.GIT_SSL_NO_VERIFY = '1';
    const git: SimpleGit = simpleGit();
    git.env(process.env);
    await git.cwd(process.env.PROJECT_DIR as string);
    let branches = await git.branch();
    let currentBranch = branches.current;
    step.info('GIT_CURRENT_BRANCH=' + currentBranch);
    if (currentBranch == 'master') {
        step.info('Current on master branch, skipping the deleting task');
        return;
    }
    try {
        await git.push('origin', currentBranch, ['--delete']);
        step.info(`Delete branch ${currentBranch} succeeded`);
    }
    catch (error) {
        step.error(`Delete branch ${currentBranch} failed`);
        throw error;
    }
}

